package com.entidades;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.CustomClasses.FetchXML;

public class Group {
	private char id;
	private List<Match> matchList;
	private List<Team> teams;
	private static List<Group> groups;
private static final String tagName = "group";
	
	private static final  String[] fieldNames = new String[]{"id", "teams"};
	
	private static final int LIST_ID = 0;
	
	private static final int LIST_TEAMS = 1;
	
	
	public static final String xmlFileName = "GroupsList.xml";
	private static final String URL = "http://www.worldcupcompendium.com/entities/GroupsList.xml";

	public List<Match> getMatchList() {
		return matchList;
	}
	public char getId() {
		return id;
	}

	Group(char id, List<Team> teams, List<Match> matchList){
		setTeams(teams);
		this.id = id;
		this.matchList = matchList;
	}
	public List<Team> getTeams() {
		return teams;
	}
	private void setTeams(List<Team> teams) {
		this.teams = teams;
	}
	
	public void getScore(int matchId){
		
	}
	
	public static List<Group> getGroups(){
		if(groups==null){
			groups = new ArrayList<Group>();
			try {
				List<List<String>> list = new FetchXML().execute(URL, tagName, fieldNames).get();
				for(List<String> l: list){		
					
					char id = l.get(LIST_ID).charAt(0);
					String[] sTeams = l.get(LIST_TEAMS).split(",");
					List<Team> teamList = new ArrayList<Team>();
					for(String s: sTeams){
					teamList.add(Team.getById(Integer.valueOf(s)));			
					}
				    
					groups.add(new Group(id, teamList, Match.getMatchOfTeams(teamList)));

					
				}	
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
		}
		return groups;
		
	}
}
